import { env } from './env';

const app = getApp();
let loadingCount = 0;
console.log( app.globalData.BaseUrl, "baseUrl");
const baseUrl = app.globalData.BaseUrl;

module.exports = {
  /**
    * url:请求的接口地址
    * method:请求方式 GET,POST....
    *  data:要传递的参数
    * showLoading 接口请求时是否显示loading，默认展示
   */
  request: (url, data, method, showLoading=true) => {
    let _url = `${baseUrl}${url}`;
    let tokenData = {};
    if (app && app.globalData.token) {
      tokenData = {
        Authorization: app.globalData.token
      }
    }
    return new Promise((resolve, reject) => {
      if (loadingCount == 0 && showLoading) {
        wx.showLoading({
          title: '正在加载',
          mask: true
        });
      }
      loadingCount++;
      wx.request({
        url: _url,
        data: {
          ...data
        },
        method: method || 'GET',
        header: {
          'content-type': 'application/json',
          ...tokenData
        },
        success: (res) => {
          let {code} = res.data;
          if (code === 200) {
            if (res.data.data && res.data.data.errorMsg) {
              wx.showToast({
                title: res.data.data.errorMsg,
                icon: "none",
                duration: 3000
              });
              return ;
            }
            resolve(res.data.data);
          } else {
            reject(res.data);
          }
        },
        fail() {
          reject('接口有误，请检查');
        },
        complete(res) {
          if (loadingCount > 0) {
            loadingCount--;
          }
          if (loadingCount === 0 && showLoading) {
            wx.hideLoading();
          }
          if (res.data) {
            if (res.data.code === 200) {

              if (res.data.data && res.data.data.errorMsg) {
                wx.showToast({
                  title: res.data.data.errorMsg,
                  icon: "none",
                  duration: 3000
                });
                return ;
              }
            } else if (res.data.code === 400) {
              wx.showToast({
                title: res.data.message || "非授权用户或用户名密码不正常",
                icon: "none",
                duration: 3000
              });
              return;
            } else if (res.data.code === 401) {

              wx.showToast({
                title: res.data.message || "登录状态超时",
                icon: "none",
                duration: 3000
              });
              wx.navigateTo({
                url: '/pages/login/login'
              });

            }  else {
              wx.showToast({
                title: res.data.message || "网络繁忙，请稍后再试",
                icon: "none",
                duration: 3000
              });
              reject(res.data);
            }
          } else {
            wx.showToast({
              title: "网络繁忙，请稍后再试",
              icon: "none",
              duration: 3000
            });
            reject(res.data);
          }
        }
      });
    });
  },
  upload: (url, file, reviews_id, showLoading=true) => {
    console.log(reviews_id, "::reviews_id::");
    let _url = `${baseUrl}${url}`;
    return new Promise((resolve, reject) => {
      if (loadingCount == 0 && showLoading) {
        wx.showLoading({
          title: '正在加载',
          mask: true
        });
      }
      loadingCount++;

      const task = wx.uploadFile({
        url: _url,
        header: {'content-type': 'multipart/form-data'},
        filePath: file.url,
        name: 'picture_file',
        formData: {
          'reviews_id': reviews_id
        },
        
        success: (res) => {
          let response_entity = JSON.parse(res.data);
          console.log(response_entity.code, "::code::");
          if (response_entity.code === 200) {
            if (response_entity.data && response_entity.data.errorMsg) {
              wx.showToast({
                title: response_entity.data.errorMsg,
                icon: "none",
                duration: 3000
              });
              return ;
            }
            resolve(response_entity.data);
          } else {
            reject(response_entity);
          }
        },
        fail(err) {
          reject('接口有误或参数有误，请检查');
          console.log(err);
        },
        complete(res) {
          if (loadingCount > 0) {
            loadingCount--;
          }
          if (loadingCount === 0 && showLoading) {
            wx.hideLoading();
          }
          if (res.data) {
            let response_entity = JSON.parse(res.data);
            if (response_entity.code === 200) {
              if (response_entity.data && response_entity.data.errorMsg) {
                wx.showToast({
                  title: response_entity.data.errorMsg,
                  icon: "none",
                  duration: 3000
                });
                return ;
              }
            } else if (response_entity.code === 401) {

              wx.showToast({
                title: response_entity.message || "登录状态超时",
                icon: "none",
                duration: 3000
              });
              wx.navigateTo({
                url: '/pages/login/login'
              });

            }  else {
              wx.showToast({
                title: response_entity.message || "网络繁忙，请稍后再试",
                icon: "none",
                duration: 3000
              });
              reject(response_entity);
            }
          } else {
            wx.showToast({
              title: "网络繁忙，请稍后再试",
              icon: "none",
              duration: 3000
            });
            reject(response_entity);
          }
        }
      })
    });
  },
};